LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-12-2003, 04:33 PM   #1
causticmtl
Member
 
Registered: May 2003
Distribution: Slackware 10.0
Posts: 41

Rep: Reputation: 15
User input using a BASH script...


Hey all,

I'm trying to write a menu in BASH that would accept a user's input with one kestroke. For instance, if the menu contained these options:

1) Beer
2) Cigarettes
3) Coffee

... a user could simply type the number three without having to press the enter key for the script to read the input. I've read through a couple of tutorials and they use the "echo -n" and "read" combination in their scripts which isn't what I want to do. I would like to minimize the amount of keystrokes a user would have to make.

I also happened upon the dialog utility to make text boxes for menus. Will this help in creating the one-keystroke functionality I'd like to have?
 
Old 07-12-2003, 09:04 PM   #2
cludwin
Member
 
Registered: Feb 2002
Distribution: Slack
Posts: 50

Rep: Reputation: 16
To change settings on in your term you have to make changes the terminal driver. You can do this by using the "stty" command. I wasn't quite sure how to read one character from the stdin so I googled for it and it seems to work well.

Hope this helps,
cludwin


#!/bin/sh

# global var to hold menu selection
MENU_CHOISE=""

function get_choise() {
##################################
# set the tty driver to raw mode #
# read a char #
# restore settings #
##################################
stty raw
MENU_CHOISE=`dd if=/dev/tty bs=1 count=1 2>/dev/null`
stty sane
echo $MENU_CHOISE
}

# print the menu
echo "Menu:"
echo " 1) booz"
echo " 2) smokes"
echo " 3) tea"
printf " please enter your choise: "

# read the menu selection
get_choise

# print the results.
case $MENU_CHOISE in

"1")
echo "Can I see some ID?";;

"2")
echo "Smoking is bad for you";;

"3")
echo "One lump or two?";;
esac
 
1 members found this post helpful.
Old 07-12-2003, 11:15 PM   #3
Daem0hn
Member
 
Registered: Jul 2003
Location: Australia
Distribution: Gentoo 1.4
Posts: 43

Rep: Reputation: 15
good tutorial

http://www.linux.org/docs/ldp/howto/...10.html#ss10.1
thats the link for the user input page, it may be worthwhile you reading the whole thing though, it is a good tutorial
 
Old 07-13-2003, 07:15 PM   #4
causticmtl
Member
 
Registered: May 2003
Distribution: Slackware 10.0
Posts: 41

Original Poster
Rep: Reputation: 15
Thanks for taking the trouble in helping me suss this out.
 
Old 07-13-2003, 09:17 PM   #5
causticmtl
Member
 
Registered: May 2003
Distribution: Slackware 10.0
Posts: 41

Original Poster
Rep: Reputation: 15
I'd really like to use Dialog and BASH if I can, however.

I have two questions concerning dialog:

Firstly, how do I get two widgets operating on the same screen? Is it even possible? I'd like to have a menu on the left-hand side containing a list of items with a running total on the right-hand side.

Secondly, where does the data from the menu go? How do I assign it to a variable that would affect the running totals on the right hand side? Reading the man pages for Dialog, output from a menu goes to something called stderr. I would need to add the dollar value of each selected item, have that value listed in the "Running total" widget on the right-hand side, and then possibly save everything to a file.

Am I better trying to do this using only BASH? Should I take a look at python-tcl/tk?
 
Old 07-13-2003, 09:59 PM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
AFAIK you can't use multiple widgets using Dialog.

I'm trying to write a menu in BASH that would accept a user's input with one kestroke.
Maybe "select"? Useless example:
select choice in quit bomb capitulate educate freeze jump k-line maim nuke obliterate submit tundra zygote; do case "$choice" in quit) echo To ${lastchoice} or not to ${lastchoice}, that is the $MYNAME dinarii question...; break;; *) lastchoice=$choice; export MYNAME=$(expr $MYNAME + $RANDOM);; esac; done
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script pipe input - bash mostly laikos Programming 4 11-09-2008 05:14 PM
Bash scripting and user input Woodsman Slackware 13 11-02-2005 02:20 PM
mask user input in a bash script PlatinumRik Linux - Software 1 06-15-2004 10:06 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
bash-script input aizkorri Programming 7 07-08-2003 06:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration